<template>
  <div>
    <van-nav-bar
      fixed
      placeholder
      safe-area-inset-top
      title="合租房间管理"
      left-arrow
      @click-left="back"
    />
    <div v-for="(roomitem, index) in room" :key="index" class="hpart">
      <div
        @click="todetail(roomitem.roomPutaway, roomitem.roomId)"
        class="roomhouse"
      >
        <img :src="roomitem.roomImages" alt="" />
        <div class="intrduce">
          <div class="title1">合租房间{{ index + 1 }}</div>
          <div class="title2">
            {{ roomitem.roomAcre }}<span>m<span class="up">2</span></span>
          </div>
          <div class="title2">{{ roomitem.roomPaymentMethod }}</div>
          <div class="title3">{{ roomitem.leasePrice }}</div>
        </div>
      </div>
      <div v-show="roomitem.roomCheckStatus == 1" class="btnpart">
        <van-button class="inbox state">等待核验</van-button>
      </div>
      <div v-show="roomitem.roomCheckStatus == 3" class="btnpart">
        <van-button class="inbox state unpass">核验未通过</van-button>
        <van-button class="borbox">重新上传</van-button>
      </div>
      <div v-show="roomitem.roomCheckStatus == 2" class="btnpart">
        <van-button class="inbox" @click="changeroom(roomitem.roomId)"
          >修改</van-button
        >
        <van-button
          v-show="roomitem.roomPutaway == 2"
          @click="showdownroom(roomitem.roomId)"
          class="borbox"
          >下架</van-button
        >
        <van-button
          v-show="roomitem.roomPutaway == 1"
          @click="uproom(roomitem.roomId)"
          class="borbox"
          >上架</van-button
        >
      </div>
    </div>
    <van-popup v-model="show">
      <div class="content">
        <div class="box1">下架房间</div>
        <div class="box2">下架后将不在享受房间推荐曝光，你确认下架房间吗？</div>
        <div class="btn">
          <button @click="downroom()">确认下架</button>
          <button @click="show = false">我再想想</button>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import { getRentownerroom, roomupdown } from "../../apis/house.js";
import local from "../../utils/local.js";
export default {
  data() {
    return {
      show: false,
      downid: "",
      room: [],
      houseId: "",
      payMargin: "",
    };
  },
  created() {
    this.houseId = this.$route.query.houseId;
    this.getlist();
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    getlist() {
      getRentownerroom({ houseId: this.houseId }).then((res) => {
        this.room = res.result.list;
        this.payMargin = res.result.payMargin;
      });
    },
    todetail(e, id) {
      if (e == 2) {
        local.set("roomId", id);
        this.$router.push({ path: "/housedetail",query:{roomId:id} });
      }
    },
    uproom(id) {
      if (this.payMargin == 1) {
        this.$toast("您需要支付房源保证金");
      } else if (this.payMargin == 2) {
        roomupdown({ put: "2", roomId: id }).then((res) => {
          if (res.code == 200) {
            this.getlist();
            this.$toast("上架成功");
          } else if (res.code == 2063) {
            this.$toast(res.msg);
          }
        });
      }
    },
    downroom() {
      roomupdown({ put: "1", roomId: this.downid }).then((res) => {
        if (res.code == 200) {
          this.getlist();
          this.show = false;
          this.$toast("下架成功");
        }
      });
    },
    showdownroom(id) {
      this.show = true;
      this.downid = id;
    },
    changeroom(id) {
      this.$router.push({ path: "/roomchange", query: { roomId: id } });
    },
  },
};
</script>

<style lang="less" scoped>
.hpart {
  position: relative;
  margin: 0 15px;
  overflow: hidden;
  padding: 10px;
  margin-bottom: 10px;
  border-radius: 10px;
  .btnpart {
    display: flex;
    flex-direction: row-reverse;
    .van-button {
      height: 35px;
      font-size: 12px;
      border-radius: 20px;
      border: none;
      padding: 0 15px;
    }
    .borbox {
      color: #45b7cb;
      border: 1px solid #45b7cb;
      margin-right: 10px;
    }
    .inbox {
      background-color: #45b7cb;
      color: #fff;
      margin-right: 0px;
    }
    .state {
      background-color: #fff;
      color: #45b7cb;
    }
    .unpass {
      color: #d43b33;
    }
  }
  .roomhouse {
    display: flex;
    margin: 10px 0px;
    img {
      width: 100px;
      height: 100px;
    }
    .intrduce {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      margin-left: 10px;
      .title1 {
        font-size: 16px;
        font-weight: 550;
        color: #333333;
      }
      .title2 {
        font-size: 12px;
        color: #a0a0a0;
        span {
          margin: 0 2px;
        }
        .up {
          font-size: 9px;
          vertical-align: super;
        }
      }
      .title3 {
        color: #d43b33;
        font-size: 16px;
        font-weight: bold;
        span {
          font-size: 12px;
          margin-left: 3px;
        }
      }
    }
  }
}
.content {
  width: 300px;
  height: 180px;
  background-color: #fff;
  text-align: center;
  .box1 {
    padding: 20px 0;
    font-size: 16px;
    color: #45b7cb;
    font-weight: bold;
  }
  .box2 {
    margin: 0 auto;
    width: 210px;
    font-size: 14px;
    color: #333;
  }
  .btn {
    margin-top: 30px;
    button {
      font-size: 14px;
      margin: 0 10px;
      width: 110px;
      height: 35px;
      border-radius: 20px;
      &:first-child {
        background-color: #fff;
        border: 1px solid #f1f1f1;
        color: #999;
      }
      &:last-child {
        background-color: #45b7cb;
        border: 1px solid #45b7cb;
        color: #fff;
      }
    }
  }
}
</style>